/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * info.java
 *
 * Created on Oct 15, 2012, 4:12:54 PM
 */
package gui;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JOptionPane;

import org.joda.time.DateMidnight;

import pms.Employee;
import pms.Supervisor;

/**
 * 
 * @author moonn52
 */
public class info extends javax.swing.JFrame {

	private Employee e;
	private ArrayList<Integer> eids;
	private ArrayList<String> employeeNames;
	
	private DateMidnight start = new DateMidnight(2012,12,2);
	ArrayList<DateMidnight> weekStarts;

	/**
	 * Creates new form info
	 * 
	 * @param e
	 */
	public info(Employee e) {
		this.e = e;
		try {
		if (e.getPriveligeLevel()>=1) {
//			System.out.println("We are a supervisor.");
			
				employeeNames = new ArrayList<String>();
				Supervisor s = new Supervisor(e.getEmployeeID());
				eids = new ArrayList<Integer> ((s).getMinions(employeeNames));
			
		} else {
//			System.out.println("We are an employee.");
			eids=new ArrayList<Integer>();
			eids.add(e.getEmployeeID());
			employeeNames = new ArrayList<String>();
			employeeNames.add(e.getName(e.getEmployeeID()));
		}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String myname = "";
		
		try {
			myname = e.getName(e.getEmployeeID());
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		if (eids.contains(e.getEmployeeID()))
			eids.remove((Object) e.getEmployeeID());
		
		if (employeeNames.contains(myname))
			employeeNames.remove(myname);
			
		eids.add(0, e.getEmployeeID());
		
		employeeNames.add(0, myname);
		
		initComponents();

		populateFields(e);
	}

	private void populateFields(Employee e) {
		// TODO Auto-generated method stub
		try {
			jtxtFirstName.setText(e.getFirstName());
			jtxtLastName.setText(e.getLastName());
			jtxtAddress1.setText(e.getAddress());
			// jtxtAddress2.setText("");
			jtxtPosition.setText("" + e.getRemainingTimeOff());
			// jtxtDepartment.setEditable(false);
			jtxtPayRate.setText(Double.toString(e.getPayRate()));
		
		} catch (Exception ex) {
			Object frame = null;
			JOptionPane.showMessageDialog((Component) frame, ex.getMessage());
		}
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		this.setTitle("Info");

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

	        jlblFirstName = new javax.swing.JLabel();
	        jtxtFirstName = new javax.swing.JTextField();
	        jtxtLastName = new javax.swing.JTextField();
	        jlblLastName = new javax.swing.JLabel();
	        jtxtAddress1 = new javax.swing.JTextField();
	        jtxtPosition = new javax.swing.JTextField();
	        jlblAddress1 = new javax.swing.JLabel();
	        jlblPosition = new javax.swing.JLabel();
	        jtxtPayRate = new javax.swing.JTextField();
	        jlblPayGrade = new javax.swing.JLabel();
	        jbtnSubmit = new javax.swing.JButton();
	        jbtnCancel = new javax.swing.JButton();
	        jbtnUnlock = new javax.swing.JButton();
	        jcmbEmployees = new javax.swing.JComboBox();
	        jcmbJobProfile = new javax.swing.JComboBox();
	        jlblJobProfile = new javax.swing.JLabel();
	        
	        jtxtFirstName.setEditable(false);
			jtxtLastName.setEditable(false);
			jtxtAddress1.setEditable(false);
			jtxtPosition.setEditable(false);
			jtxtPayRate.setEditable(false);
			jcmbJobProfile.setEnabled(false);

	        jlblFirstName.setText("First Name");

	        jtxtFirstName.setColumns(25);

	        jtxtLastName.setColumns(25);

	        jlblLastName.setText("Last Name");

	        jlblAddress1.setText("Address");

	        jlblPosition.setText("PTO");

	        jlblPayGrade.setText("Pay Grade");

	        jbtnSubmit.setText("Submit");
	        jbtnSubmit.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jbtnSubmitActionPerformed(evt);
	            }
	        });

	        jbtnCancel.setText("Cancel");
	        jbtnCancel.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jbtnCancelActionPerformed(evt);
	            }
	        });

	        jbtnUnlock.setText("Unlock");
	        jbtnUnlock.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jbtnUnlockActionPerformed(evt);
	            }
	        });

	        jcmbEmployees.setModel(new javax.swing.DefaultComboBoxModel(employeeNames.toArray()));
	        jcmbEmployees.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jcmbEmployeesActionPerformed(evt);
	            }
	        });
	        Supervisor s;
	        Vector<String> jobNames = new Vector<String>();
	        Vector<Integer> jobID = new Vector<Integer>();
	        Vector<Double> minPay = new Vector<Double>();
	        Vector<Double> maxPay = new Vector<Double>();
	        int selPos = 0;
	        try {
	        	if (e.getPriveligeLevel() >= 1) {
	        		s = new Supervisor(e.getEmployeeID());
	        		jobID = s.getAllJobs(jobNames, minPay, maxPay);
	        		selPos = e.getjobID() - 1;
	        	} else {
	        		jobID.add(e.getEmployeeID());
	        		jobNames.add(e.getJobName());
	        		double pay = e.getPayRate();
	        		minPay.add(pay);
	        		maxPay.add(pay);
	        		selPos = 0;
	        	}
	        } catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}	
	        
	        
	        
	        jcmbJobProfile.setModel(new javax.swing.DefaultComboBoxModel(jobNames.toArray()));
	        jcmbJobProfile.setSelectedIndex(selPos);

	        jlblJobProfile.setText("Job Profile");

	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(layout.createSequentialGroup()
	                        .addComponent(jbtnCancel, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addComponent(jbtnUnlock, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addComponent(jbtnSubmit, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
	                        .addGap(6, 6, 6))
	                    .addGroup(layout.createSequentialGroup()
	                        .addGap(9, 9, 9)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addComponent(jcmbEmployees, javax.swing.GroupLayout.Alignment.TRAILING, 0, 409, Short.MAX_VALUE)
	                            .addGroup(layout.createSequentialGroup()
	                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
	                                    .addComponent(jlblAddress1)
	                                    .addComponent(jlblLastName)
	                                    .addComponent(jlblFirstName)
	                                    .addComponent(jlblPosition)
	                                    .addComponent(jlblJobProfile)
	                                    .addComponent(jlblPayGrade))
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                                    .addComponent(jtxtPayRate, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
	                                    .addComponent(jtxtPosition, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
	                                    .addComponent(jtxtFirstName, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
	                                    .addComponent(jtxtAddress1, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
	                                    .addComponent(jtxtLastName, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
	                                    .addComponent(jcmbJobProfile, 0, 321, Short.MAX_VALUE))))))
	                .addContainerGap())
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(jcmbEmployees, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jtxtFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(jlblFirstName))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jlblLastName)
	                    .addComponent(jtxtLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jtxtAddress1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(jlblAddress1))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jtxtPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(jlblPosition))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jcmbJobProfile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(jlblJobProfile))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jtxtPayRate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(jlblPayGrade))
	                .addGap(20, 20, 20)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jbtnCancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                    .addComponent(jbtnUnlock, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                    .addComponent(jbtnSubmit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
	                .addContainerGap())
	        );

	        pack();
	    }// </editor-fold>

	    private void jbtnSubmitActionPerformed(java.awt.event.ActionEvent evt) {
	        // TODO add your handling code here:
	    	int n = JOptionPane.showConfirmDialog(null, "Change your info?",
					"Are you sure?", JOptionPane.YES_NO_OPTION);
			if (n != 1) {
				Employee k = null;
				try {
					k = new Employee(eids.get(jcmbEmployees.getSelectedIndex()));
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				k.editPersonalInfo(jtxtFirstName.getText(), jtxtLastName.getText(), jtxtAddress1.getText());
				this.dispose();
			}
	    }

	    private void jbtnCancelActionPerformed(java.awt.event.ActionEvent evt) {
	        // TODO add your handling code here:
	    	this.dispose();
	    }

	    private void jbtnUnlockActionPerformed(java.awt.event.ActionEvent evt) {
	        // TODO add your handling code here:
	    	jbtnUnlock.setText(jbtnUnlock.getText().equals("Unlock") ? "Lock" : "Unlock"); 
			jtxtFirstName.setEditable(!jtxtFirstName.isEditable());
			jtxtLastName.setEditable(!jtxtLastName.isEditable());
			jtxtAddress1.setEditable(!jtxtAddress1.isEditable());
//			jtxtAddress2.setEditable(!jtxtAddress2.isEditable());
			
//			jtxtDepartment.setEditable(!jtxtDepartment.isEditable());
			if (Employee.accessLevel.fromInteger(e.getPriveligeLevel()) == Employee.accessLevel.PAYROLL_ADMIN || Employee.accessLevel.fromInteger(e.getPriveligeLevel()) == Employee.accessLevel.MANAGER) {
				jtxtPayRate.setEditable(!jtxtPayRate.isEditable());
				jtxtPosition.setEditable(!jtxtPosition.isEditable());
//				jcmbJobProfile.setEnabled(!jcmbJobProfile.isEnabled());
			}
	    }
	    
	    private void jcmbEmployeesActionPerformed(java.awt.event.ActionEvent evt) {
	        // TODO add your handling code here:
	    	try {
				Employee e = new Employee(eids.get(jcmbEmployees.getSelectedIndex()));
				populateFields(e);
				jcmbJobProfile.setSelectedIndex(e.getjobID() - 1);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }


	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(info.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(info.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(info.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(info.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				Employee k = null;
				try {
//					k = new Employee("BalrogKiller", "elfears");
//					k = new Employee("MagicMan", "poweruser");
//					k = new Employee("NorseMaiden", "valkyrja");
					k = new Supervisor("th0r", "THORSMASH");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				new info(k).setVisible(true);

			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton jbtnCancel;
    private javax.swing.JButton jbtnSubmit;
    private javax.swing.JButton jbtnUnlock;
    private javax.swing.JComboBox jcmbEmployees;
    private javax.swing.JComboBox jcmbJobProfile;
    private javax.swing.JLabel jlblAddress1;
    private javax.swing.JLabel jlblFirstName;
    private javax.swing.JLabel jlblJobProfile;
    private javax.swing.JLabel jlblLastName;
    private javax.swing.JLabel jlblPayGrade;
    private javax.swing.JLabel jlblPosition;
    private javax.swing.JTextField jtxtAddress1;
    private javax.swing.JTextField jtxtFirstName;
    private javax.swing.JTextField jtxtLastName;
    private javax.swing.JTextField jtxtPayRate;
    private javax.swing.JTextField jtxtPosition;

	// End of variables declaration//GEN-END:variables
}
